package com.inspectortime;

import junit.framework.TestCase;

import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

/**
 * Makes sure the Spring applicationContext and springmvc-servlet files can be
 * loaded properly
 * 
 */
public abstract class BaseSpringContextAwareTestCase extends TestCase {

	private ContextLoader loader = null;

	private MockServletContext sc = null;

	protected WebApplicationContext ctx = null;

	/**
	 * Instantiates WebApplicationContext ctx. Note that the
	 * spring-envo-unit-test.xml file enables unit tests to use a set of
	 * external configurations which is different from the one used with web
	 * servers.
	 * 
	 * @see junit.framework.TestCase#setUp()
	 */
	public void setUp() throws Exception {
		super.setUp();

		if (ctx == null) {
			loader = new ContextLoader();
			sc = new MockServletContext("");

			String[] applicationContextPaths = new String[] {
					"src/main/webapp/WEB-INF/spring-db.xml",
					"src/main/webapp/WEB-INF/spring-mvc-servlet.xml",
					"src/main/webapp/WEB-INF/spring-mail.xml",
					"src/test/resources/spring-envo-unit-test.xml"};
			String contexts = "";
			for (int i = 0; i < applicationContextPaths.length; i++) {
				contexts += "file:" + applicationContextPaths[i] + ",";
			}
			sc.addInitParameter("contextConfigLocation", contexts);
			ctx = loader.initWebApplicationContext(sc);
		}
	}

	public void tearDown() throws Exception {
		super.tearDown();
		if (loader != null && sc != null) {
			loader.closeWebApplicationContext(sc);
		}
	}

}
